import { getCategoryAPI } from '@/apis/categoryAPI'
//引入路由的useRoute方法，以便获取路由参数
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { onMounted, ref } from 'vue'
export function useCategory() {
    //调用接口获取数据
const categoryData = ref({})
const route = useRoute()
//id默认值为路由参数params.id
//当路由参数变化时，通过to.params.id获取最新的id
const getCategory = async (id=route.params.id) => {
  const res = await getCategoryAPI(id)
  categoryData.value = res.result
}

//【路由参数变化时，重新发送分类数据接口】
// 参数可写to,from,next
//to目标路由，from来自路由，next下一步（放行)
onBeforeRouteUpdate((to)=>{
getCategory(to.params.id)
//console.log(to);
})

onMounted(() => {
  getCategory()
  
})
return{
    categoryData
}
}
